<?php
/**
 * Created by 绵阳人维网络科技有限公司.
 * User: 罗东华
 * Date: 2016/5/13
 * Time: 14:41
 */
namespace Libs;
class Category{
    public $array;
    public $str;
    public $a='&nbsp;&nbsp;&nbsp;│&nbsp;';
    public $b="&nbsp;&nbsp;&nbsp;├─&nbsp;";
    public $c='&nbsp;&nbsp;&nbsp;└─&nbsp;';
    //额外数组
    public $data;

    public function __construct($array){
             $this->array=$array;

    }

    //相同父id排一起
    public function array_order(){
        if(!$this->array){return false;}
        foreach($this->array as $v){
            $arr[$v['parentid']][]=$v;
        }
        $arr=$this->Category_order($arr[0],$arr);

        return $arr;


    }


    /*递归菜单*/
    public function Category_order($menu,&$data,$a=0){
        foreach($menu as $k=> &$v){
            //东哥神奇的 遍历记录递归次数
            $b=$a;
            $v['leep']=$a;
            $b++;
            if($data[$v['catid']]){

                $menu[$k]['child']=$this->Category_order($data[$v['catid']],$data,$b);
            }

        }
        return $menu;
    }

    //栏目字符串
    public function cate_str($arr){
        if(!$arr){return false;}
        $count=count($arr);
        $i=1;
        foreach($arr as $k=> $v){
           if($v['leep']){
               $st=$this->b;
               if($i==$count){ $st=$this->c;}
           }
            if($v['type']==1){
                $type='外部栏目';
            }else{
                $type='内部栏目';
            }
            $this->str.= '<tr>
	        <td align="center"><input name="listorders['.$v['catid'].']" type="text" size="3" value="'.$v['listorder'].'" class="input-text radius btn"></td>
	        <td align="center"><font color="">'.$v['catid'].'</font></td>
	        <td>'.str_repeat($this->a,$v['leep']-1).$st.$v['catname'].'</td>
	        <td align="center">'.$type.'</td>
	        <td>'.$this->data[$v['modelid']].'</td>
	        <td align="center"><a href="'.$v['url'].'" target="_blank">访问</a></td>
	        <td align="center">域名</td>
	        <td align="center"><a href="'.U("Category/edit",array('pid'=>$v['catid'])).'">添加子栏目</a> | <a href="'.U("Category/edit",array('catid'=>$v['catid'])).'">修改</a> | <a class="getajax_ldh del"  msg="删除栏目？"  _href="'.U("Category/del",array('catid'=>$v['catid'])).'">删除</a> </td>
	        </tr>';


            if($v['child']){
                $this->cate_str($v['child']);
            }
            $i++;
        }


    }
    //上家栏目字符串
    public function pid_str($arr){
        if(!$arr){return false;}
        $count=count($arr);
        $i=1;
        foreach($arr as $k=> $v){
            if($v['leep']){
                $st=$this->b;
                if($i==$count){ $st=$this->c;}
            }
            $this->str.= '<option value="'.$v["catid"].'"> '.str_repeat($this->a,$v['leep']-1).$st.$v['catname'].'</option> ';
            if($v['child']){
                $this->pid_str($v['child']);
            }
            $i++;
        }


    }



    public function lists_str($type=0){
        if(!$this->array){return false;}
        $arr=$this->array_order();
        if($type==1){
            $this->pid_str($arr);
        }else{
            $this->cate_str($arr);

        }


        return $this->str;

    }

}